package com.fertshop.gui.listener;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import com.fertshop.dao.FertishopDao;
import com.fertshop.dto.ShopDto;
import com.fertshop.exception.FieldEmptyException;
import com.fertshop.exception.NoRecordException;
import com.fertshop.gui.Main;
import com.fertshop.gui.MainPanel;

public class SearchSubmitActionListener implements ActionListener{
	
	private JTextField salepointDoorNo;
	private JTextField salepointVillage;
	private JDialog dialog;
	private JDesktopPane desktop;
	public static JInternalFrame updateShopIf;
	
	public SearchSubmitActionListener(JTextField salepointDoorNo,JTextField salepointVillage, 
			JDialog dialog,JDesktopPane desktop){
		this.salepointDoorNo=salepointDoorNo;
		this.salepointVillage=salepointVillage;
		this.dialog=dialog;
		this.desktop=desktop;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		try{
			if(salepointDoorNo.getText().isEmpty()){
				throw new FieldEmptyException("Sale Point Door Number Cannot be Empty.");
			}
			if(salepointVillage.getText().isEmpty()){
				throw new FieldEmptyException("Sale Point Area Cannot be Empty.");
			}
			dialog.dispose();
			FertishopDao shopdao=new FertishopDao();
			ShopDto shopdto=shopdao.searchShop(salepointDoorNo.getText(), salepointVillage.getText());
			Main.updatedto=shopdto;
			
			updateShopIf=new JInternalFrame("Update Shop");
			MainPanel updatePanel=new MainPanel();
			updatePanel.getSubmit().setEnabled(false);
			
			updatePanel.getShopNameTf().setText(shopdto.getShopName());			
			updatePanel.getProprietorNameTf().setText(shopdto.getProprietorName());			
			updatePanel.getLicenseNoTf().setText(shopdto.getLicenseNumber());			
			updatePanel.getAddressTa().setText(shopdto.getAddress());			
			updatePanel.getDoorNoTf().setText(shopdto.getSpDoorNo());			
			updatePanel.getAreaTf().setText(shopdto.getSpArea());			
			updatePanel.getSpDoorNoTf().setText(shopdto.getStrgpDoorNo());			
			updatePanel.getSpAreaTf().setText(shopdto.getStrgpArea());
			
			
			updatePanel.setPreferredSize(new Dimension(600,600));
			JScrollPane scroll=new JScrollPane(updatePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
												ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			scroll.setPreferredSize(new Dimension(400,400));
			updateShopIf.setContentPane(scroll);
			desktop.add(updateShopIf);		
			updateShopIf.setClosable(true);
			updateShopIf.setMaximizable(true);
			updateShopIf.setResizable(true);
			updateShopIf.setIconifiable(true); 
			try { 
				updateShopIf.setSelected(true); 
		 	} catch (java.beans.PropertyVetoException e2) { 
		 	  } 
			updateShopIf.setSize(500, 500);
			updateShopIf.setVisible(true);
		}catch(Exception e){
			if(e instanceof FieldEmptyException){				
				showEmptyMessageDialog(e);
			}
			
			if(e instanceof NoRecordException){
				showEmptyMessageDialog(e);
			}
		}
		
	}
	
	/**
	 * This method shows message dilog and displays message from exception.
	 * @param e
	 */
	private void showEmptyMessageDialog(Exception e){
		JOptionPane optionpane=new JOptionPane(e.getMessage());
		JDialog dialog=optionpane.createDialog(Main.frame, "Message");		
		dialog.setVisible(true);
	}

}
